package chujisuanfa;



import java.util.HashSet;
import java.util.Set;

public class Permutation {
    Set<String> set = new HashSet<>();
    StringBuilder sb = new StringBuilder();
    int[] f; // 0 表示没被加入sb，1表示被加入sb
    public String[] permutation(String s) {
        f = new int[s.length()];
        dfs(s);
        return set.toArray(new String[set.size()]);
    }
    public void dfs(String s){
        if (sb.length()==s.length()){
            set.add(sb.toString());
            return;
        }
        for (int i=0;i<s.length();++i){
            if (f[i]==1) continue;
            sb.append(s.charAt(i));
            f[i] =1;
            dfs(s);
            sb.deleteCharAt(sb.length()-1);
            f[i]=0;
        }

    }

    public static void main(String[] args) {
        Permutation permutation = new Permutation();
        System.out.println(permutation.permutation("abc")[0]);
    }

}
